package com.feidee.data.report.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
  
public class JsonUtil {  
  
    /** 
     * 对象转换成JSON字符串 
     *  
     * @param obj 
     *            需要转换的对象 
     * @return 对象的string字符 
     */  
    public static String toJson(Object obj) {  
        JSONObject jSONObject = JSONObject.fromObject(obj);  
        return jSONObject.toString();  
    }  
  
    /** 
     * JSON字符串转换成对象 
     *  
     * @param jsonString 
     *            需要转换的字符串 
     * @param type 
     *            需要转换的对象类型 
     * @return 对象 
     */  
    @SuppressWarnings("unchecked")  
    public static <T> T fromJson(String jsonString, Class<T> type) {  
        JSONObject jsonObject = JSONObject.fromObject(jsonString);  
        return (T) JSONObject.toBean(jsonObject, type);  
    }  
  
    /** 
     * 将JSONArray对象转换成list集合 
     *  
     * @param jsonArr 
     * @return 
     */  
    public static List<Object> jsonToList(JSONArray jsonArr) {  
        List<Object> list = new ArrayList<Object>();  
        for (Object obj : jsonArr) {  
            if (obj instanceof JSONArray) {  
                list.add(jsonToList((JSONArray) obj));  
            } else if (obj instanceof JSONObject) {  
                list.add(jsonToMap((JSONObject) obj));  
            } else {  
                list.add(obj);  
            }  
        }  
        return list;  
    }  
  
    /** 
     * 将json字符串转换成map对象 
     *  
     * @param json 
     * @return 
     */  
    public static Map<String, Object> jsonToMap(String json) {  
        JSONObject obj = JSONObject.fromObject(json);  
        return jsonToMap(obj);  
    } 
    
    public static Map<String, String> jsonToStrMap(String json) {  
        JSONObject obj = JSONObject.fromObject(json);  
        return jsonToStrMap(obj);  
    }
  
    /** 
     * 将JSONObject转换成map对象 
     *  
     * @param obj
     * @return 
     */  
    public static Map<String, String> jsonToStrMap(JSONObject obj) {  
        Set<?> set = obj.keySet();  
        Map<String, String> map = new HashMap<String, String>(set.size());  
        for (Object key : obj.keySet()) {  
            map.put(key.toString(), obj.get(key).toString());  
        }  
        return map;  
    }  
    
    
    /** 
     * 将JSONObject转换成map对象 
     *  
     * @param obj
     * @return 
     */  
    public static Map<String, Object> jsonToMap(JSONObject obj) {  
        Set<?> set = obj.keySet();  
        Map<String, Object> map = new HashMap<String, Object>(set.size());  
        for (Object key : obj.keySet()) {
            Object value = obj.get(key);
            if (value instanceof JSONArray) {  
                map.put(key.toString(), jsonToList((JSONArray) value));  
            } else if (value instanceof JSONObject) {  
                map.put(key.toString(), jsonToMap((JSONObject) value));  
            } else {  
                map.put(key.toString(), obj.get(key));  
            }  
  
        }  
        return map;  
    }  
    
	public static String listMap2JsonStr(List<Map<String, Object>> lm){
    	String jsonStr = JSONArray.fromObject(lm).toString();
	    JSONArray ja = JSONArray.fromObject(jsonStr);
		return ja.toString();
    }
	public static String map2JsonStr(Map<String, String[]> lm){
		@SuppressWarnings("deprecation")
		JSONObject jsonObject = JSONObject.fromMap(lm); 
		return jsonObject.toString();
    }
	
	@SuppressWarnings("unchecked")
	public static List<Map<String,Object>> jsonStr2ListMap(String js){
		JSONArray ja = JSONArray.fromObject(js);
		return JSONArray.toList(ja, HashMap.class);
    }
	public static void main(String[] args) {
//		 Map<String,Object> map1 = new HashMap<String,Object>();
//	        Map<String,Object> map2 = new HashMap<String,Object>();
//	        Map<String,Object> map3 = new HashMap<String,Object>();
//	        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//	        map1.put("name","小明");
//	        map1.put("age","23");
//	        map1.put("sex","男");
//	        list.add(map1);
//
//	        map2.put("name","小王");
//	        map2.put("age","24");
//	        map2.put("sex","女");
//	        list.add(map2);
//
//	        map3.put("name","小张");
//	        map3.put("age","22");
//	        map3.put("sex","男");
//	        list.add(map3);
//		System.out.println(JsonUtil.listMap2JsonStr(list));
//		List<Map<String, Object>> lm = jsonStr2ListMap(JsonUtil.listMap2JsonStr(list));
//		for (Map<String, Object> map : lm) {
//			System.out.println(map.get("name"));
//		}

//    String line ="\n" +
//        "\n" +
//        "    { \"programmers\": \"aaaaa\",\n" +
//        "\n" +
//        "    \"authors\": [\n" +
//        "\n" +
//        "    { \"firstName\": \"Isaac\", \"lastName\": \"Asimov\", \"genre\": \"science fiction\" },\n" +
//        "\n" +
//        "    { \"firstName\": \"Tad\", \"lastName\": \"Williams\", \"genre\": \"fantasy\" },\n" +
//        "\n" +
//        "    { \"firstName\": \"Frank\", \"lastName\": \"Peretti\", \"genre\": \"christian fiction\" }\n" +
//        "\n" +
//        "    ],\n" +
//        "\n" +
//        "    \"musicians\": [\n" +
//        "\n" +
//        "    { \"firstName\": \"Eric\", \"lastName\": \"Clapton\", \"instrument\": \"guitar\" },\n" +
//        "\n" +
//        "    { \"firstName\": \"Sergei\", \"lastName\": \"Rachmaninoff\", \"instrument\": \"piano\" }\n" +
//        "\n" +
//        "    ] }\n";
//    Map<String, Object> map = JsonUtil.jsonToMap(line);
//   List list = (List) map.get("authors");
//    System.out.println(((Map<String,String>)list.get(0)).get("firstName"));
  }
}  